package com.lanqiao.attendance.controller;


import com.github.pagehelper.PageInfo;
import com.lanqiao.attendance.pojo.Administrator;
import com.lanqiao.attendance.service.AdminService;
import com.lanqiao.attendance.utils.JWTUtils;
import com.lanqiao.attendance.vo.LoginParam;
import com.lanqiao.attendance.vo.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;


@Api
@RestController
@RequestMapping("/admin")
public class AdminController {
    @Autowired
    AdminService adminService;

    /**
     * 管理员注册
     * @param administrator
     * @return
     */
    @ApiOperation("管理员注册功能")
    @PostMapping("/register")
    public Result register(@RequestBody Administrator administrator){
        // 对密码进行md5加密处理
        String admPassword = administrator.getAdmPassword();
        admPassword = org.apache.commons.codec.digest.DigestUtils.md5Hex(admPassword+"shzu");
        administrator.setAdmPassword(admPassword);
        //System.out.println(administrator);
        // 插入数据库
        int register = adminService.register(administrator);
        if(register>0){
            return Result.success("注册成功",register);
        }
        else {
            return Result.fail(201,"注册失败");
        }
    }

    /**
     * 管理员登陆
     * @param administrator
     * @return
     */


    /**
     * 添加管理员
     * @param administrator
     * @return
     */
    @ApiOperation("管理员添加功能")
    @PutMapping("/Admin")
    public Result addAdmin(@RequestBody Administrator administrator){
        // 对密码进行md5加密处理
        String admPassword = administrator.getAdmPassword();
        admPassword = org.apache.commons.codec.digest.DigestUtils.md5Hex(admPassword+"shzu");
        administrator.setAdmPassword(admPassword);
        // 插入数据库
        boolean b = adminService.addAdmin(administrator);
        if(b){
            return Result.success("添加成功",b);
        }else {
            return Result.fail(201,"添加失败");
        }
    }

    /**
     * 删除管理员
     * @param id
     * @return
     */
    @ApiOperation("管理员删除功能")
    @DeleteMapping
    public Result deleteAdminById(long id){
        boolean deleteAdmin = adminService.deleteAdmin(id);
        if(deleteAdmin){
            return Result.success("删除成功",deleteAdmin);
        }else {
            return Result.fail(201,"删除失败");
        }

    }

    /**
     *更新
     * @param administrator
     * @return
     */
    @ApiOperation("管理员更新功能")
    @PostMapping("/update")
    public Result updateAdmin(@RequestBody Administrator administrator){
        // 对密码进行md5加密处理
        System.out.println(administrator);
        String admPassword = administrator.getAdmPassword();
        admPassword = DigestUtils.md5Hex(admPassword+"shzu");
        administrator.setAdmPassword(admPassword);

        //更新
        int row = adminService.updateAdmin(administrator);
        if(row>0){
            return Result.success("更新成功",row);
        }else {
            return Result.fail(201,"更新失败");
        }
    }

    /**
     * 查询全部,显示管理员列表
     * @return
     */
    @ApiOperation("显示管理员列表")
    @GetMapping("/list/{pageNum}/{pageSize}")
    public Result selectAll(@PathVariable("pageNum") Integer pageNum,@PathVariable("pageSize")Integer pageSize){
        PageInfo<Administrator> pageInfo = adminService.selectAll(pageNum, pageSize);
        //System.out.println(pageInfo);
        if(pageInfo!=null){
            return Result.success("查询成功",pageInfo);
        }else {
            return Result.fail(201,"查询失败");
        }
    }


    @ApiOperation("根据用户名模糊查询")
    @GetMapping("/like")
    public Result findByPage(@RequestParam(value = "pageNum",required = false,defaultValue = "1") Integer pageNum,
                             @RequestParam(value = "pageSize",required = false,defaultValue = "10") Integer pageSize,
                             @RequestParam(value = "admUsername") String  admUserName
    ){
        PageInfo<Administrator> pageInfo = adminService.selectByLikeName(pageNum, pageSize, admUserName);
        if(pageInfo!=null){
            return Result.success("搜索成功",pageInfo);
        }else {
            return Result.fail(201,"搜索失败");
        }
    }

    @ApiOperation("根据id查询")
    @GetMapping("/{id}")
    public Result selectById(@PathVariable long id){
        Administrator administrator = adminService.selectById(id);
        if(administrator!=null){
            return Result.success("成功",administrator);
        }else {
            return Result.fail(201,"失败");
        }

    }


    @PostMapping("queryByAccount")
    public Result queryByAccount(@RequestBody LoginParam administrator){
        String account = administrator.getAccount();
        System.out.println(account);
        return adminService.queryByAccount(account);
    }



}
